package com.niiwoo.civet.mobile.controller.myCustomer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.mobile.vo.request.myCustomer.*;
import com.niiwoo.civet.mobile.vo.response.myCustomer.*;
import com.niiwoo.civet.user.dto.request.NiiwooScorePageRequestDTO;
import com.niiwoo.civet.user.dto.request.myCustomer.*;
import com.niiwoo.civet.user.dto.response.myCustomer.*;
import com.niiwoo.civet.user.service.myCustomer.MyCustomerInfoDubboService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.web.annotation.AuthIgnore;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Empty;
import com.niiwoo.tripod.web.vo.PageRequestVO;
import com.niiwoo.tripod.web.vo.PageResponseVO;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by laichunjing  2018.04.23
 */
@RestController
@RequestMapping("/myCustomer")
@Api(tags = "我的客服(赖春景)", description = "/myCustomer")
public class MyCustomerController {

    @Reference(version = "1.0.0")
    private MyCustomerInfoDubboService myCustomerInfoDubboService;

    @PostMapping("/myCustomerHomePage")
    @ApiOperation("我的客服首页信息")
    @AuthIgnore
    public Result<MyCustomerIndexQueryResponseVO> myCustomerHomePage() {
        String userId = PrincipalContext.getUserId();
        MyCustomerIndexQueryResponseVO responseVO=new MyCustomerIndexQueryResponseVO();
        MyCustomerHomePageResponseDTO dto = myCustomerInfoDubboService.myCustomerHomePageList(userId);
        responseVO.setIdentityTag(dto.getIdentityStatus());
        responseVO.setUserName(dto.getUserName());
        responseVO.setIsNewReceive(dto.getIsNewReceive());
        List<MyCustomerIndexQueryResponseVO.Problem> investHotProblemsList=new ArrayList<>();
        List<MyCustomerIndexQueryResponseVO.Problem> borrowHotProblemsList=new ArrayList<>();
        List<MyCustomerIndexQueryResponseVO.Keyword> keyWordList=new ArrayList<>();

        for(ProblemResponseDTO problemResponseDTO: dto.getInvestHotProblemsList()){
            MyCustomerIndexQueryResponseVO.Problem problemVo=new MyCustomerIndexQueryResponseVO.Problem();
            problemVo.setId(problemResponseDTO.getId());
            problemVo.setProblemName(problemResponseDTO.getProblemName());
            problemVo.setProblemStatus(problemResponseDTO.getRelationType());
            problemVo.setIsNew(problemResponseDTO.getIsNew());
            investHotProblemsList.add(problemVo);
        }
        responseVO.setInvestHotProblemsList(investHotProblemsList);
        for(ProblemResponseDTO problemResponseDTO: dto.getBorrowHotProblemsList()){
            MyCustomerIndexQueryResponseVO.Problem problemVo=new MyCustomerIndexQueryResponseVO.Problem();
            problemVo.setId(problemResponseDTO.getId());
            problemVo.setProblemName(problemResponseDTO.getProblemName());
            problemVo.setProblemStatus(problemResponseDTO.getRelationType());
            problemVo.setIsNew(problemResponseDTO.getIsNew());
            borrowHotProblemsList.add(problemVo);
        }
        responseVO.setBorrowHotProblemsList(borrowHotProblemsList);
        for(KeyWordResponseDTO keyWordResponseDTO: dto.getKeywordList()){
            MyCustomerIndexQueryResponseVO.Keyword keywordVo=new MyCustomerIndexQueryResponseVO.Keyword();
            keywordVo.setId(keyWordResponseDTO.getId());
            keywordVo.setKeyWordName(keyWordResponseDTO.getKeyWordName());
            keywordVo.setKeyWordImgUrl(keyWordResponseDTO.getKeyWordImgUrl());
            keyWordList.add(keywordVo);
        }
        responseVO.setKeywordList(keyWordList);

        return Result.with(responseVO);
    }

    @PostMapping("/searchProblem")
    @ApiOperation("问题搜索")
    @AuthIgnore
    public Result<SearchProbelmResponseVO> searchProblem(@RequestBody @Valid SearchProblemRequestVO vo) {
        SearchProbelmResponseVO responseVO =new SearchProbelmResponseVO();
        List<SearchProbelmResponseVO.Problem> problemVOList=new ArrayList<>();
        QueryContentResponseDTO dto=myCustomerInfoDubboService.queryProblemsList(vo.getSearchContent());
        dto.getProblemList().forEach(res->{
            SearchProbelmResponseVO.Problem problemVO=new  SearchProbelmResponseVO.Problem();
            problemVO.setId(res.getId());
            problemVO.setProblemName(res.getProblemTitle());
            problemVOList.add(problemVO);
        });
        responseVO.setProblemsList(problemVOList);
        return Result.with(responseVO);
    }

    @PostMapping("/problemList")
    @ApiOperation("关键词查询问题列表")
    @AuthIgnore
    public Result<ProblemListByKeywordResponseVO> problemList(@RequestBody @Valid KeywordQueryRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        ProblemListByKeywordResponseVO reponseVO =new ProblemListByKeywordResponseVO();
        ProblemListResponseDTO problemListResponseDTO=myCustomerInfoDubboService.queryProblemListByKeyword(vo.getKeyWordId(),userId);
        reponseVO.setIdentityTag(problemListResponseDTO.getIdentityStatus());
        List<ProblemResponseDTO> borrowHotProblemsList = problemListResponseDTO.getBorrowHotProblemsList();
        List<ProblemListByKeywordResponseVO.Problem> problemsList=new ArrayList<>();
        borrowHotProblemsList.forEach(res->{
            ProblemListByKeywordResponseVO.Problem problemVO=new ProblemListByKeywordResponseVO.Problem();
            problemVO.setId(res.getId());
            problemVO.setProblemName(res.getProblemName());
            problemVO.setIsNew(res.getIsNew());
            problemsList.add(problemVO);
        });
        reponseVO.setProblemsList(problemsList);
        return Result.with(reponseVO);
    }
    @PostMapping("/keyWordList")
    @ApiOperation("关键词列表")
    @AuthIgnore
    public Result<KeyWordQueryResponseVO> problemList() {
        String userId = PrincipalContext.getUserId();
        KeyWordListResponseDTO keyWordListResponseDTO = myCustomerInfoDubboService.queryKeyWordList(userId);
        KeyWordQueryResponseVO responseVO = new KeyWordQueryResponseVO();
        List<KeyWordQueryResponseVO.Keyword> KeywordVOList = new ArrayList<>();
        responseVO.setIdentityTag(keyWordListResponseDTO.getIdentityStatus());
        for (KeyWordResponseDTO KeyWordResponseDTO : keyWordListResponseDTO.getKeywordList()) {
            KeyWordQueryResponseVO.Keyword keywordVo = new KeyWordQueryResponseVO.Keyword();
            keywordVo.setId(KeyWordResponseDTO.getId());
            keywordVo.setKeyWordName(KeyWordResponseDTO.getKeyWordName());
            keywordVo.setKeyWordImgUrl(KeyWordResponseDTO.getKeyWordImgUrl());
            keywordVo.setKeyWordActiveImgUrl(KeyWordResponseDTO.getKeyWordActiveImgUrl());
            KeywordVOList.add(keywordVo);
        }
        responseVO.setKeywordList(KeywordVOList);
        return Result.with(responseVO);
    }
    @PostMapping("/showQuestionDetail")
    @ApiOperation("查看问题答案")
    @AuthIgnore
    public Result<AnswerResponseVO> showQuestionDetail(@RequestBody @Valid AnswerQueryRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        AnswerResponseVO answerResponseVO=new AnswerResponseVO();
        List<AnswerResponseVO.ProblemContentResponseVO> problemList=new ArrayList<>();
        ProblemDetailResponseDTO problemDetailResponseDTO=myCustomerInfoDubboService.showQuestionDetail(vo.getQuestionId(),vo.getKeywordId(),userId);
        answerResponseVO.setId(problemDetailResponseDTO.getId());
        answerResponseVO.setProblemName(problemDetailResponseDTO.getProblemName());
        answerResponseVO.setAnswerContent(problemDetailResponseDTO.getAnswerContent());
        answerResponseVO.setAnswerStatus(problemDetailResponseDTO.getAnswerStatus());
        List<ProblemContentResponseDTO> problemContentResponseDTOList=problemDetailResponseDTO.getProblemList();
        problemContentResponseDTOList.forEach(res->{
            AnswerResponseVO.ProblemContentResponseVO problemContentResponseVO=new AnswerResponseVO.ProblemContentResponseVO();
            problemContentResponseVO.setId(res.getId());
            problemContentResponseVO.setProblemName(res.getProblemTitle());
            problemList.add(problemContentResponseVO);
        });
        answerResponseVO.setProblemList(problemList);
        return Result.with(answerResponseVO);
    }

    @PostMapping("/updateAnswerStatus")
    @ApiOperation("修改有用无用状态")
    public Result<AnswerStatusResponseVO> updateAnswerStatus(@RequestBody @Valid AnswerStatusQueryRequestVO vo) {
        AnswerStatusResponseVO responseVO=new AnswerStatusResponseVO();
        String userId = PrincipalContext.getUserId();
        ProblemStatusRequestDTO problemStatusRequestDTO=new ProblemStatusRequestDTO();
        problemStatusRequestDTO.setUserId(userId);
        problemStatusRequestDTO.setQuestionId(vo.getQuestionId());
        problemStatusRequestDTO.setStatus(vo.getStatus());
        ProblemStatusResponseDTO responseDTO=myCustomerInfoDubboService.updateAnswerStatus(problemStatusRequestDTO);
        responseVO.setAnswerStatus(responseDTO.getAnswerStatus());
        return Result.with(responseVO);
    }

    @PostMapping("/accountPVAndUV")
    @ApiOperation("统计问题/关键词 PV，UV")
    @AuthIgnore
    public Empty accountPVAndUV(@RequestBody @Valid AccountPVAndUVRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        AccountPVAndUVRequestDTO accountPVAndUVRequestDTO=new AccountPVAndUVRequestDTO();
        accountPVAndUVRequestDTO.setUserId(userId);
        accountPVAndUVRequestDTO.setQuestionId(vo.getQuestionId());
        accountPVAndUVRequestDTO.setKeywordId(vo.getKeywordId());
        accountPVAndUVRequestDTO.setChannel(vo.getChannel());
        accountPVAndUVRequestDTO.setDeviceId(vo.getDeviceId());
        accountPVAndUVRequestDTO.setEventId(vo.getEventId());
        accountPVAndUVRequestDTO.setPage(vo.getPage());
        accountPVAndUVRequestDTO.setTime(vo.getTime());
        accountPVAndUVRequestDTO.setReferer(vo.getReferer());
        accountPVAndUVRequestDTO.setSessionId(vo.getSessionId());
        if(!myCustomerInfoDubboService.accountPVAndUV(accountPVAndUVRequestDTO)){
            throw new BizException("USR10041");
        }
        return Empty.getInstance();
    }

    @PostMapping("/saveFeedBackInfo")
    @ApiOperation("保存反馈记录信息")
    public Empty saveFeedBackInfo(@RequestBody @Valid FeedBackSolveStatusRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        NewFeedBackSaveRequestDTO newFeedBackSaveRequestDTO=new NewFeedBackSaveRequestDTO();
        newFeedBackSaveRequestDTO.setUserId(userId);
        newFeedBackSaveRequestDTO.setType(vo.getFeedbackType());
        newFeedBackSaveRequestDTO.setAppVersion(vo.getAppVersion());
        newFeedBackSaveRequestDTO.setDeviceType(vo.getDeviceType());
        newFeedBackSaveRequestDTO.setContent(vo.getFeedBackContent());
        newFeedBackSaveRequestDTO.setDeviceSystem(vo.getDeviceSystem());
        List<String> feedBackImgUrlVO=vo.getFeedBackImgUrl();
        List<String> feedBackImgUrl=new ArrayList<>();
        feedBackImgUrlVO.forEach(res->{
            feedBackImgUrl.add(res);
        });
        newFeedBackSaveRequestDTO.setFeedBackImgUrl(feedBackImgUrl);
        if(!myCustomerInfoDubboService.saveFeedBackInfo(newFeedBackSaveRequestDTO)){
            throw new BizException("USR10024");
        }
        return Empty.getInstance();
    }


    @PostMapping("/feedBackRecord")
    @ApiOperation("反馈记录")
    public Result<PageResponseVO<FeedBackInfoResponseVO>> feedBackRecord(@RequestBody @Valid PageRequestVO requestVO) {
        String userId = PrincipalContext.getUserId();
        NiiwooScorePageRequestDTO requestDTO=new NiiwooScorePageRequestDTO();
        requestDTO.setUserId(userId);
        requestDTO.setPageNumber(requestVO.getPageNumber());
        requestDTO.setPageSize(requestVO.getPageSize());
        PageResponseDTO<FeedBackInfoResponseDTO> pageDTO = myCustomerInfoDubboService.showFeedBackRecord(requestDTO);
        PageResponseVO<FeedBackInfoResponseVO> pageVO=new PageResponseVO<FeedBackInfoResponseVO>();
        if(pageDTO!=null){
            List<FeedBackInfoResponseDTO> list=pageDTO.getItems();
            List<FeedBackInfoResponseVO> voList=new ArrayList<>();
            list.forEach(res->{
                FeedBackInfoResponseVO feedBackInfoResponseVO=new FeedBackInfoResponseVO();
                feedBackInfoResponseVO.setId(res.getId());
                feedBackInfoResponseVO.setFeedBackTitle(res.getFeedBackTitle());
                feedBackInfoResponseVO.setFeedBackStatus(res.getFeedBackStatus());
                feedBackInfoResponseVO.setFeedBackSubStatus(res.getFeedBackSubStatus());
                feedBackInfoResponseVO.setFeedBackTitle(res.getFeedBackTitle());
                feedBackInfoResponseVO.setIsImage(res.getIsImage());
                feedBackInfoResponseVO.setIsNewReceived(res.getIsNewReceived());
                feedBackInfoResponseVO.setFeedBackTime(res.getFeedBackTime());
                voList.add(feedBackInfoResponseVO);
            });
            pageVO.setItems(voList);
            pageVO.setTotalPage(pageDTO.getTotalPage());
            pageVO.setTotalCount(pageDTO.getTotalCount());

        }
        return Result.with(pageVO);
    }

    @PostMapping("/feedBackDetails")
    @ApiOperation("反馈详情")
    public Result<FeedBackDetailsResponseVO> feedBackDetails(@RequestBody @Valid FeedBackDetailsRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        FeedBackDetailsResponseVO responseVO=new FeedBackDetailsResponseVO();
        FeedBackDetailRequestDTO feedBackDetailRequestDTO=new FeedBackDetailRequestDTO();
        feedBackDetailRequestDTO.setId(vo.getId());
        List<FeedBackDetailsResponseVO.AppendOrReplyContent> appendOrReplyContentListVO=new ArrayList<>();
        FeedBackDetailResponseDTO responseDTO=myCustomerInfoDubboService.feedBackDetails(feedBackDetailRequestDTO);
        List<AppendOrReplyContent> appendOrReplyContentList=responseDTO.getAppendOrReplyContentList();
        responseVO.setId(responseDTO.getId());
        responseVO.setFeedBackContent(responseDTO.getFeedBackContent());
        responseVO.setFeedBackImgUrl(responseDTO.getFeedBackImgUrl());
        responseVO.setFeedBackType(responseDTO.getFeedBackType());
        responseVO.setFeedBackStatus(responseDTO.getFeedBackStatus());
        responseVO.setFeedBackSubStatus(responseDTO.getFeedBackSubStatus());
        responseVO.setFeedBackTime(responseDTO.getFeedBackTime());
        appendOrReplyContentList.forEach(res->{
            FeedBackDetailsResponseVO.AppendOrReplyContent appendOrReplyContent=new FeedBackDetailsResponseVO.AppendOrReplyContent();
            appendOrReplyContent.setAppendType(res.getAppendType());
            appendOrReplyContent.setAppendContent(res.getAppendContent());
            appendOrReplyContent.setAppendImgUrl(res.getAppendImgUrl());
            appendOrReplyContent.setAppendTime(res.getAppendTime());
            appendOrReplyContentListVO.add(appendOrReplyContent);
        });
        responseVO.setAppendOrReplyContentList(appendOrReplyContentListVO);
        return Result.with(responseVO);
    }

    @PostMapping("/appendFeedBackInfo")
    @ApiOperation("追加反馈描述")
    public Empty appendFeedBackInfo(@RequestBody @Valid AppendFeedBackInfoRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        AppendFeedBackRequestDTO requestDTO=new AppendFeedBackRequestDTO();
        requestDTO.setId(vo.getId());
        requestDTO.setFeedBackContent(vo.getFeedBackContent());
        requestDTO.setFeedBackImgUrl(vo.getFeedBackImgUrl());
        if(!myCustomerInfoDubboService.appendFeedBackInfo(requestDTO)){
            throw new BizException("USR10040");
        }
        return Empty.getInstance();
    }

    @PostMapping("/feedBackSolveStatus")
    @ApiOperation("反馈解决状态")
    public Empty feedBackSolveStatus(@RequestBody @Valid FeedBackInfoRequestVO vo) {
        String userId = PrincipalContext.getUserId();
        FeedBackStatusRequestDTO feedBackStatusRequestDTO=new FeedBackStatusRequestDTO();
        feedBackStatusRequestDTO.setId(vo.getId());
        feedBackStatusRequestDTO.setStatus(vo.getStatus());
        if(! myCustomerInfoDubboService.feedBackSolveStatus(feedBackStatusRequestDTO)){
            throw new BizException("USR10039");
        }
        return Empty.getInstance();
    }

    @PostMapping("/feedBackisNewReceive")
    @ApiOperation("反馈是否最新回复")
    @AuthIgnore
    public Result<FeedBackIsNewReceiveResponseVO> feedBackisNewReceive() {
        String userId = PrincipalContext.getUserId();
        FeedBackIsNewReceiveResponseVO vo=new FeedBackIsNewReceiveResponseVO();
        FeedBackIsNewReceiveResponseDTO feedBackIsNewReceiveResponseDTO= myCustomerInfoDubboService.feedBackisNewReceive(userId);
        vo.setIsNewReceive(feedBackIsNewReceiveResponseDTO.getIsNewReceive());
        return Result.with(vo);
    }

}
